package ird.demo.controller;

import ird.demo.common.DemoConstant;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
public abstract class BaseController {
	@Autowired
	protected MessageSource msgSrc;
	
	protected abstract String getTc();
	
	protected abstract Map<String, String> doValidateInput(Map paraMap);
	
	protected abstract void retrieveDetail(HttpServletRequest request, ModelMap model);
	
	protected String getMsg(String msgCode){
		return msgSrc.getMessage(msgCode, null, "Default", null);
	}
	
	protected String getMsg(String msgCode, String[] params){
		return msgSrc.getMessage(msgCode, params, "Default", null);
	}
	
	protected String getBasePath(){
		return DemoConstant.PATH_SEPERATOR + getTc() + DemoConstant.PATH_SEPERATOR;
	}
	
	@RequestMapping(value=DemoConstant.PATH_SEPERATOR+DemoConstant.INPUT, method=RequestMethod.GET)
	protected String input(ModelMap model) {
		System.out.println("==============go input222");
		
		String userId = "testUser1";
		model.addAttribute("currentTime", DemoConstant.TIME_FORMAT.format(new Date()));
		model.addAttribute("userId", userId);
		return getBasePath()+DemoConstant.INPUT;
	}
	
	@RequestMapping(value=DemoConstant.PATH_SEPERATOR+DemoConstant.DETAIL, method=RequestMethod.POST)
	protected ModelAndView detail(HttpServletRequest request, ModelMap model) {
		System.out.println("==============go detail33, paraMap="+request.getParameterMap());
		retrieveDetail(request, model);
//		
//		if(model.get(DemoConstant.ERR_CODE) != null){
//			System.out.println("=======model="+model);
//			return new ModelAndView(new RedirectView(request.getContextPath() + DemoConstant.PATH_SEPERATOR + getBasePath()+DemoConstant.INPUT), model);
//		}
//		System.out.println("result="+result);
//		model.addAttribute(DemoConstant.DETAIL_VALUE, result);
//		return getBasePath()+DemoConstant.DETAIL;
		return new ModelAndView(getBasePath()+DemoConstant.DETAIL, model);
	}
	
	@RequestMapping(DemoConstant.PATH_SEPERATOR+DemoConstant.DONE)
	protected String done(ModelMap model) {
		
		return getBasePath()+DemoConstant.DONE;
	}
	
	@RequestMapping(DemoConstant.PATH_SEPERATOR+DemoConstant.BINGO)
	protected String bingo(ModelMap model) {
		return getBasePath()+DemoConstant.BINGO;
	}
	
	@RequestMapping(DemoConstant.PATH_SEPERATOR+DemoConstant.ABEND)
	protected String abend() {
		return getBasePath()+DemoConstant.ABEND;
	}
	
	
//	@RequestMapping(value=DemoConstant.PATH_SEPERATOR+DemoConstant.VALIDATE_INPUT)
//	@ResponseBody
//	public String validateInput(HttpServletRequest request, ModelMap model) {
//		System.out.println("==============go validateInput"+request.getParameter("prn"));
//		doValidateInput(request, model);
//		return gson.toJson(model);
//	}
	
	@RequestMapping(value=DemoConstant.PATH_SEPERATOR+DemoConstant.VALIDATE_INPUT)
	@ResponseBody
	public Map<String, String> validateInput(@RequestBody Map paraMap){
		System.out.println("==============go validateInput:"+paraMap);
		return doValidateInput(paraMap);
	}
}
